
//
//  SampuxEquipmentInstallView.m
//  Sampux
//
//  Created by rzm on 16/7/6.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxEquipmentInstallView.h"
#import "DeviceMessageViewCell.h"
#import "ModeSettingViewCell.h"
#import "DataSynchronizeTableViewCell.h"
//#import "MJRefreshNormalHeader.h"

@interface SampuxEquipmentInstallView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, strong) NSArray *imgArr;
@property (nonatomic, strong) UILabel *alertLab;

@end

@implementation SampuxEquipmentInstallView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _dataArr = [NSMutableArray array];
        [self setupSubView];
    }
    return self;
}

- (void)setupSubView
{
    _installTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.width, self.height - 64) style:UITableViewStylePlain];
    [self addSubview:_installTableView];
    _installTableView.backgroundColor = THSystemClearColor;
    _installTableView.dataSource = self;
    _installTableView.delegate = self;
    [_installTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_installTableView registerNib:[UINib nibWithNibName:@"ModeSettingViewCell" bundle:nil] forCellReuseIdentifier:@"modeCell"];
    [_installTableView registerNib:[UINib nibWithNibName:@"DeviceMessageViewCell" bundle:nil] forCellReuseIdentifier:@"messageCell"];
     _header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshDataWithHeader)];
    _installTableView.mj_header = _header;
    [_installTableView.mj_header beginRefreshing];
}

- (void)refreshDataWithHeader
{
    if (self.model != nil) {
        [GBInterMaster_CommForSystem requestDeviceConfig:0 withDeviceAccount:self.model.accountStr];
    }else
    {
        [_installTableView.mj_header endRefreshing];
    }
   
}

- (void)setDataArr:(NSMutableArray *)dataArr
{
    dispatch_async(dispatch_get_main_queue(), ^{
        _dataArr = dataArr;
        [_installTableView reloadData];
        [_installTableView.mj_header endRefreshing];
    });
}

#pragma mark UITableViewCellDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.configModel.isPossessor == 1) {
        _titleArr = @[@"定时设置",@"权限设置",@"运行记录",@"解除绑定"];
        _imgArr = @[@"time_set",@"permissions_set",@"logout_set",@"release_set"];
    }else
    {
        _titleArr = @[@"定时设置",@"运行记录",@"解除绑定"];
        _imgArr = @[@"time_set",@"logout_set",@"release_set",];
    }
       return _titleArr.count + 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 3) {
        DataSynchronizeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dataCell"];
        if (cell == nil) {
            cell = [[DataSynchronizeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"dataCell"];
        }
//        cell.segment.delegate = self;
        if (self.dataArr.count == 0) {
        }else
        {
            cell.configModel =  self.dataArr[0];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = THWhiteAlplaColor(0.1);
        return cell;
    }else if (indexPath.row >= 0 &&indexPath.row < 3)
    {
        NSArray *titleArr = @[@"设备ID",@"设备别名",@"设备所有者"];
        NSArray *imgArr = @[@"device_set",@"alisa_set",@"user_set"];
        DeviceMessageViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"messageCell"];
        if (cell == nil)
        {
            cell = [[DeviceMessageViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"messageCell"];
        }
        cell.nameLab.text = titleArr [indexPath.row];
        [cell.imgView setImage:imageNamed(imgArr[indexPath.row]) forState:UIControlStateNormal];
//            NSArray *dataArr = @[self.model.accountStr,self.model.nameStr,@"小白"];
        if (self.dataArr.count == 0) {
        } else {
            SPDeviceConfigModel *configModel = self.dataArr[0];
            if (configModel.alias.length == 0) {
                configModel.alias = configModel.deviceAccount;
            } else {
                configModel.alias = configModel.alias;
            }
            NSArray *deviceDataArr = @[configModel.deviceAccount,configModel.alias,configModel.ownAccount];
            cell.lineView.hidden = YES;

            cell.deviceStr = deviceDataArr[indexPath.row];
            if (self.configModel.isPossessor == YES) {
                cell.deviceLab.text = cell.deviceStr;

                if (indexPath.row == 1) {
                    cell.lineView.hidden = NO;
                }
            } else {
                if (indexPath.row == 1) {
                    cell.lineView.hidden = YES;
//                    cell.deviceLab.text = cell.deviceStr;
                }
            }
        }
        cell.backgroundColor = THWhiteAlplaColor(0.1);
        return cell;
    } else {
        ModeSettingViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"modeCell"];
        cell.nameLabel.text = self.titleArr[indexPath.row - 4];
        [cell.iconImgView setImage:imageNamed(self.imgArr[indexPath.row - 4]) forState:UIControlStateNormal];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = THWhiteAlplaColor(0.1);
        return cell;
    }
}

#pragma mark UITableViewCellDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (indexPath.row == 4 ||indexPath.row == 5) {
        cell.backgroundColor = THWhiteAlplaColor(0.36);
    }else
    {
        cell.backgroundColor = THWhiteAlplaColor(0.1);
    }
    if (self.deviceDelegate && [self.deviceDelegate respondsToSelector:@selector(seletedRow:)]) {
        [self.deviceDelegate seletedRow:indexPath];
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.backgroundColor = THWhiteAlplaColor(0.1);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.installTableView)
    {
        CGFloat sectionHeaderHeight = Sampux6HScale(50);
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}

- (void)setConfigModel:(SPDeviceConfigModel *)configModel
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [_installTableView.mj_header endRefreshing];
        _configModel = configModel;
        [_installTableView reloadData];
    });

}

@end
